<template>
  <div class="app-container">

    <el-card class="box-card">
      <el-form  :rules="rules"  ref="ruleForm" :model="ruleForm"  label-width="150px" v-loading='loading' >
        <el-form-item label="分类类型："  prop="id" >
          <el-select   v-model="ruleForm.id" placeholder="请选择" @change="pidchange">
                      <el-option
                        v-for="item in positionList"
                              :key="item.id"
                              :label="item.names"
                              :value="item.id"
                      >
                      </el-option>
                  </el-select>
        </el-form-item>

        <el-form-item label="分类名称：" prop="names">
          <el-input v-model="ruleForm.names" />
        </el-form-item>

       


        <el-form-item>
            <el-button @click="handleCancel">取消</el-button>
            <el-button :loading='loading' type="primary" @click="handlePublish">保存</el-button>
        </el-form-item>

      </el-form>
    </el-card>

  </div>
</template>



<script>
import api from '@/api/material/category'

export default {
  name: 'categoryCreate',
  data() {
    return {
        loading:false,
        positionList:'',
        ruleForm: {
          names:'',
          pid:'',
        },
       rules:{
          names: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
          id:[{ required: true, message: '请选择类型类别', trigger: 'change' }],
       },

    }
  },
  computed: {
   
  },
  created() {
   
  },
  mounted() {
      this.clist()
  },
  methods: {
  
    pidchange(e){
  
      for (let  i=0; i<this.positionList.length; i++) {
        if(this.positionList[i].id==e){
            this.ruleForm.pid=this.positionList[i].id
            return
        }
      }
    
    },

    handleCancel: function() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.push({ path: './list' })
    },
    handlePublish() {

       this.$refs["ruleForm"].validate((valid) => {
          if (valid) {
              this.Create(this.ruleForm)
          } else {
            this.$message({
              message: '请检查数据必填项',
              type: 'warning'
            });
            return false
          }
        })
    },
  clist() {
      api.clist({
        limit:1000,
        page:1
      }).then(res => {
        if(res.data.errno==0){
             
             this.positionList=res.data.data.list
             this.positionList.unshift({
              id: 0, pid: 0, names: '一级目录',
             })
            }
      }).catch(() => {
      })
    },
  Create(e) {
      this.loading = true
      api.create(e).then(res => {
          if(res.data.errno==0){
            this.loading = false
            this.$message({
            message: '新增成功',
            type: 'success'
          });
            this.$router.push({ path: './list' })
          }
      }).catch((e) => {
        this.loading = false
        this.$message({
              message: e.data,
              type: 'warning'
            });
      })
    },

  }
}
</script>
<style lang="scss" scoped>

  
</style>